﻿namespace Utility1
{
    using System;
    using System.Data;
    using System.Globalization;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Text;

    public class DBConvert
    {
        private const byte BYTE_MIN_VALUE = 0;
        private static char CHAR_MIN_VALUE = '\0';
        private static DateTime DATETIME_MIN_VALUE = DateTime.MinValue;
        [DecimalConstant(0, 0x80, uint.MaxValue, uint.MaxValue, uint.MaxValue)]
        private static readonly decimal DECIMAL_MIN_VALUE = -79228162514264337593543950335M;
        private const double DOUBLE_MIN_VALUE = double.MinValue;
        private const int INT_MIN_VALUE = -2147483648;
        private const long LONG_MIN_VALUE = -9223372036854775808L;
        private const short SMALLINT_MIN_VALUE = -32768;
        private static TimeSpan TIMESPAN_MIN_VALUE = TimeSpan.MinValue;

        private static object ConvertByteArrToObject(byte[] o, TypeCode t)
        {
            object obj2 = o;
            if (t == TypeCode.String)
            {
                obj2 = Encoding.UTF8.GetString(o);
            }
            return obj2;
        }

        private static bool HasCoulmn(IDataReader dre, string column)
        {
            try
            {
                return (dre.GetOrdinal(column) >= 0);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ParseBool(object value)
        {
            try
            {
                return ((value != null) ? Convert.ToBoolean(value) : false);
            }
            catch
            {
                return false;
            }
        }

        public static byte ParseByte(object value)
        {
            try
            {
                return ((value != null) ? Convert.ToByte(value) : ((byte) 0));
            }
            catch
            {
                return 0;
            }
        }

        public static byte ParseByte(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return byte.Parse(value);
            }
            catch
            {
                return 0;
            }
        }

        public static DateTime ParseDateTime(string value, string format)
        {
            DateTimeFormatInfo provider = (DateTimeFormatInfo) CultureInfo.CurrentUICulture.DateTimeFormat.Clone();
            provider.ShortDatePattern = format;
            provider.FullDateTimePattern = format;
            try
            {
                return DateTime.Parse(value, provider);
            }
            catch
            {
                return DATETIME_MIN_VALUE;
            }
        }

        public static byte ParseDBToByte(IDataReader dre, string column)
        {
            int num = -2147483648;
            byte num2 = 0;
            try
            {
                num = dre.IsDBNull(dre.GetOrdinal(column)) ? 0 : 1;
                if (num > 0)
                {
                    num2 = byte.Parse(dre.GetString(dre.GetOrdinal(column)));
                }
            }
            catch (Exception)
            {
            }
            return num2;
        }

        public static char ParseDBToChar(IDataReader dre, string column)
        {
            char ch = '\0';
            try
            {
                ch = dre.IsDBNull(dre.GetOrdinal(column)) ? CHAR_MIN_VALUE : char.Parse(dre.GetString(dre.GetOrdinal(column)));
            }
            catch (Exception)
            {
            }
            return ch;
        }

        public static DateTime ParseDBToDateTime(IDataReader dre, string column)
        {
            DateTime minValue = DateTime.MinValue;
            try
            {
                minValue = dre.IsDBNull(dre.GetOrdinal(column)) ? DATETIME_MIN_VALUE : dre.GetDateTime(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return minValue;
        }

        public static decimal ParseDBToDecimal(IDataReader dre, string column)
        {
            decimal num = -79228162514264337593543950335M;
            try
            {
                num = dre.IsDBNull(dre.GetOrdinal(column)) ? -79228162514264337593543950335M : dre.GetDecimal(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return num;
        }

        public static double ParseDBToDouble(IDataReader dre, string column)
        {
            double minValue = double.MinValue;
            try
            {
                minValue = dre.IsDBNull(dre.GetOrdinal(column)) ? double.MinValue : dre.GetDouble(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return minValue;
        }

        public static int ParseDBToInt(IDataReader dre, string column)
        {
            int num = -2147483648;
            try
            {
                num = dre.IsDBNull(dre.GetOrdinal(column)) ? -2147483648 : int.Parse(dre.GetInt32(dre.GetOrdinal(column)).ToString());
            }
            catch (Exception)
            {
            }
            return num;
        }

        public static long ParseDBToLong(IDataReader dre, string column)
        {
            long num = -9223372036854775808L;
            try
            {
                num = dre.IsDBNull(dre.GetOrdinal(column)) ? -9223372036854775808L : dre.GetInt64(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return num;
        }

        public static object ParseDBToObject(IDataReader dre, PropertyInfo property)
        {
            object obj2 = null;
            Type propertyType = property.PropertyType;
            switch (Type.GetTypeCode(propertyType))
            {
                case TypeCode.Boolean:
                    obj2 = false;
                    break;

                case TypeCode.Char:
                    obj2 = TIMESPAN_MIN_VALUE;
                    break;

                case TypeCode.Byte:
                    obj2 = (byte) 0;
                    break;

                case TypeCode.Int16:
                    obj2 = (short) (-32768);
                    break;

                case TypeCode.Int32:
                    obj2 = -2147483648;
                    break;

                case TypeCode.Double:
                    obj2 = -1.7976931348623157E+308;
                    break;

                case TypeCode.Decimal:
                    obj2 = -79228162514264337593543950335M;
                    break;

                case TypeCode.DateTime:
                    obj2 = DATETIME_MIN_VALUE;
                    break;

                case TypeCode.String:
                    obj2 = string.Empty;
                    break;
            }
            try
            {
                if (!(!HasCoulmn(dre, property.Name) || dre.IsDBNull(dre.GetOrdinal(property.Name))))
                {
                    obj2 = dre.GetValue(dre.GetOrdinal(property.Name));
                }
                else
                {
                    string column = property.Name.Replace("Desc", string.Empty);
                    if (!(!HasCoulmn(dre, column) || dre.IsDBNull(dre.GetOrdinal(column))))
                    {
                        obj2 = dre.GetValue(dre.GetOrdinal(column));
                    }
                }
            }
            catch (Exception exception)
            {
                Write2Log.WriteLogs("DBConvert", "ParseDBToObject(IDataReader dre, PropertyInfo property)", exception.Message);
            }
            if ((obj2 != null) && (obj2.GetType() == typeof(byte[])))
            {
                obj2 = ConvertByteArrToObject((byte[]) obj2, Type.GetTypeCode(propertyType));
            }
            return obj2;
        }

        public static short ParseDBToSmallInt(IDataReader dre, string column)
        {
            short num = -32768;
            try
            {
                num = dre.IsDBNull(dre.GetOrdinal(column)) ? ((short) (-32768)) : dre.GetInt16(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return num;
        }

        public static string ParseDBToString(IDataReader dre, string column)
        {
            string str = string.Empty;
            try
            {
                str = dre.IsDBNull(dre.GetOrdinal(column)) ? string.Empty : dre.GetString(dre.GetOrdinal(column));
            }
            catch (Exception)
            {
            }
            return str;
        }

        public static decimal ParseDecimal(object value)
        {
            try
            {
                return ((value != null) ? Convert.ToDecimal(value) : -79228162514264337593543950335M);
            }
            catch
            {
                return -79228162514264337593543950335M;
            }
        }

        public static decimal ParseDecimal(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return decimal.Parse(value);
            }
            catch
            {
                return -79228162514264337593543950335M;
            }
        }

        public static double ParseDouble(object value)
        {
            try
            {
                return Convert.ToDouble(value);
            }
            catch
            {
                return double.MinValue;
            }
        }

        public static double ParseDouble(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return double.Parse(value);
            }
            catch
            {
                return double.MinValue;
            }
        }

        public static int ParseInt(object value)
        {
            try
            {
                return ((value != null) ? Convert.ToInt32(value) : -2147483648);
            }
            catch
            {
                return -2147483648;
            }
        }

        public static int ParseInt(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return int.Parse(value);
            }
            catch
            {
                return -2147483648;
            }
        }

        public static long ParseLong(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return long.Parse(value);
            }
            catch
            {
                return -9223372036854775808L;
            }
        }

        public static bool? ParseNullableBool(object value)
        {
            try
            {
                return (bool?) value;
            }
            catch
            {
                return null;
            }
        }

        public static short ParseSmallInt(string value)
        {
            value = value.Replace(",", "").Trim();
            try
            {
                return short.Parse(value);
            }
            catch
            {
                return -32768;
            }
        }

        public static string ParseString(byte value)
        {
            return ((value == 0) ? string.Empty : value.ToString());
        }

        public static string ParseString(char value)
        {
            return ((value == CHAR_MIN_VALUE) ? string.Empty : value.ToString());
        }

        public static string ParseString(decimal value)
        {
            return ((value == -79228162514264337593543950335M) ? string.Empty : value.ToString());
        }

        public static string ParseString(double value)
        {
            return ((value == double.MinValue) ? string.Empty : value.ToString());
        }

        public static string ParseString(short value)
        {
            return ((value == -32768) ? string.Empty : value.ToString());
        }

        public static string ParseString(int value)
        {
            return ((value == -2147483648) ? string.Empty : value.ToString());
        }

        public static string ParseString(long value)
        {
            return ((value == -9223372036854775808L) ? string.Empty : value.ToString());
        }

        public static string ParseString(object value)
        {
            string str;
            try
            {
                switch (Type.GetTypeCode(value.GetType()))
                {
                    case TypeCode.Char:
                        return ParseString((char) value);

                    case TypeCode.Byte:
                        return ParseString((byte) value);

                    case TypeCode.Int16:
                        return ParseString((short) value);

                    case TypeCode.Int32:
                        return ParseString((int) value);

                    case TypeCode.Int64:
                        return ParseString((long) value);

                    case TypeCode.Double:
                        return ParseString((double) value);

                    case TypeCode.Decimal:
                        return ParseString((decimal) value);
                }
                return value.ToString();
            }
            catch (Exception)
            {
                str = string.Empty;
            }
            return str;
        }

        public static string ParseString(DateTime value, string format)
        {
            return ((value == DATETIME_MIN_VALUE) ? string.Empty : value.ToString(format));
        }

        public static object ParseToDBValue(bool value)
        {
            return value;
        }

        public static object ParseToDBValue(char value)
        {
            return ((value == CHAR_MIN_VALUE) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(DateTime value)
        {
            return ((value == DATETIME_MIN_VALUE) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(decimal value)
        {
            return ((value == -79228162514264337593543950335M) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(double value)
        {
            return ((value == double.MinValue) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(short value)
        {
            return ((value == -32768) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(int value)
        {
            return ((value == -2147483648) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(long value)
        {
            return ((value == -9223372036854775808L) ? ((object) DBNull.Value) : ((object) value));
        }

        public static object ParseToDBValue(object _value)
        {
            if (_value != null)
            {
                Type type = _value.GetType();
                if (type == typeof(char))
                {
                    return ParseToDBValue((char) _value);
                }
                if (type == typeof(string))
                {
                    return ParseToDBValue((string) _value);
                }
                if (type == typeof(byte))
                {
                    return ParseToDBValue((short) ((byte) _value));
                }
                if (type == typeof(short))
                {
                    return ParseToDBValue((short) _value);
                }
                if (type == typeof(int))
                {
                    return ParseToDBValue((int) _value);
                }
                if (type == typeof(long))
                {
                    return ParseToDBValue((long) _value);
                }
                if (type == typeof(decimal))
                {
                    return ParseToDBValue((decimal) _value);
                }
                if (type == typeof(double))
                {
                    return ParseToDBValue((double) _value);
                }
                if (type == typeof(DateTime))
                {
                    return ParseToDBValue((DateTime) _value);
                }
                if (type == typeof(bool))
                {
                    return ParseToDBValue((bool) _value);
                }
            }
            return null;
        }

        public static object ParseToDBValue(string value)
        {
            return (((value == null) || (value == string.Empty)) ? ((object) DBNull.Value) : ((object) value));
        }

        public static string SlitYoutube(string value)
        {
            string str = value.Split(new char[] { '=' })[1];
            return str.Split(new char[] { '&' })[0];
        }
    }
}

